package net.ib.mtalk.ui.attachment.sound;

import java.io.IOException;

import net.ib.mtalk.R;
import net.ib.mtalk.base.BaseActivity;
import net.ib.mtalk.util.MTalkLogWriter;
import android.content.Intent;
import android.media.MediaPlayer;
import android.net.Uri;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.Toast;

public class AudioPlayActivity extends BaseActivity {

	private MediaPlayer mediaPlayer = new MediaPlayer();

	private Button resetButton;
	private Button startButton;
	private Button stopButton;

	protected OnClickListener resetButtonListener = new OnClickListener() {
		@Override
		public void onClick(View v) {
			resetAction();
		}
	};

	protected OnClickListener startButtonListener = new OnClickListener() {
		@Override
		public void onClick(View v) {
			startAction();
		}
	};

	protected OnClickListener stopButtonListener = new OnClickListener() {
		@Override
		public void onClick(View v) {
			stopAction();
		}
	};

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);

		initUI();

		try {
			Intent intent = getIntent();
			Uri fileUri = intent.getData();

			mediaPlayer.setDataSource(this, fileUri);
			mediaPlayer.prepare();
			
			resetButton.setClickable(false);
			startButton.setClickable(true);
			stopButton.setClickable(true);
		} catch (IOException e) {
			Toast.makeText(this, "Can not prepare audio.", Toast.LENGTH_SHORT).show();
			MTalkLogWriter.e(e);
		}
	}

	@Override
	public void onDestroy() {
		super.onDestroy();

		if(mediaPlayer != null){
			mediaPlayer.release();
			mediaPlayer = null;
		}
	}

	/**
	 * 변하지 않는 UI 요소들
	 */
	private void initUI() {
		setContentView(R.layout.layout_attachment_play_audio);

		resetButton = (Button) findViewById(R.id.btn_reset_play_audio);
		startButton = (Button) findViewById(R.id.btn_start_play_audio);
		stopButton = (Button) findViewById(R.id.btn_stop_play_audio);

		resetButton.setOnClickListener(resetButtonListener);
		startButton.setOnClickListener(startButtonListener);
		stopButton.setOnClickListener(stopButtonListener);

		resetButton.setClickable(false);
		startButton.setClickable(false);
		stopButton.setClickable(false);
	}

	private void resetAction() {
		try {
			mediaPlayer.stop();
			mediaPlayer.prepare();

			resetButton.setClickable(false);
			startButton.setClickable(true);
			stopButton.setClickable(true);
		} catch(Exception e) {
			MTalkLogWriter.e(e);
			Toast.makeText(AudioPlayActivity.this, "Fail", Toast.LENGTH_SHORT).show();
		}
	}

	private void startAction() {
		try {
			mediaPlayer.start();

			resetButton.setClickable(true);
			startButton.setClickable(false);
			stopButton.setClickable(true);
		} catch(Exception e) {
			MTalkLogWriter.e(e);
			Toast.makeText(AudioPlayActivity.this, "Fail", Toast.LENGTH_SHORT).show();
		}
	}

	private void stopAction() {
		try {
			mediaPlayer.release();

			resetButton.setClickable(false);
			startButton.setClickable(false);
			stopButton.setClickable(false);

			finish();
		} catch(Exception e) {
			MTalkLogWriter.e(e);
			Toast.makeText(AudioPlayActivity.this, "Fail", Toast.LENGTH_SHORT).show();
		}
	}

	private void cancelAction() {
		if (mediaPlayer == null)
			return;

		mediaPlayer.reset();
		mediaPlayer.release();
		mediaPlayer = null;
	}

}
